﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BPM.Base.Tools;
using System.ServiceModel;
using BPM.Foundation.WFDB;
using System.ServiceModel.Description;
using System.Xml;
using BPM.Foundation.PWDB;

namespace BPM.RepotrService
{
   public class WCFService
    {

        private ServiceHost ServiceHost;

        private Uri serverUrl;

        private WFDBDataContext wfDataContext;
        private PWDBDataContext pwDataContext;

        public RpService wfService;

        //http://localhost:7656/RPDB/
        public WCFService(Uri serverUrl)
        {
            try
            {
                string connectionString = MyConfig.getConnection("myconstring");
                if (!string.IsNullOrEmpty(connectionString))
                {
                    wfDataContext = new WFDBDataContext(connectionString);
                    pwDataContext = new PWDBDataContext(connectionString);
                }
                else
                {
                    wfDataContext = new WFDBDataContext();
                    pwDataContext = new PWDBDataContext();
                }
                wfDataContext.SubmitChanges();
                pwDataContext.SubmitChanges();
                MyLog.writeInfo("数据库连接成功:" + wfDataContext.Connection.ConnectionString);


                this.serverUrl = serverUrl;

                wfService = new RpService(wfDataContext, pwDataContext);

                ServiceHost = new ServiceHost(wfService, serverUrl);

                ServiceHost.Closed += new EventHandler(host_Closed);
                ServiceHost.Opened += new EventHandler(host_Opened);
                ServiceHost.UnknownMessageReceived += new EventHandler<UnknownMessageReceivedEventArgs>(host_UnknownMessageReceived);
                ServiceHost.Faulted += new EventHandler(host_Faulted);


                ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
                behavior.HttpGetEnabled = true;
                ServiceHost.Description.Behaviors.Add(behavior);
                System.ServiceModel.BasicHttpBinding bh = new BasicHttpBinding();
                XmlDictionaryReaderQuotas quotas = new XmlDictionaryReaderQuotas();


                quotas.MaxStringContentLength = 500000000;
                quotas.MaxArrayLength = 500000000;
                quotas.MaxBytesPerRead = 500000000;
                bh.ReaderQuotas = quotas;
                bh.MaxReceivedMessageSize = 500000000;



                ServiceHost.AddServiceEndpoint(typeof(IRpService), bh, "RP/");


            }

            catch (System.Exception ex)
            {

                MyLog.writeError(ex.Message);

                throw new MyException(ex.Message);
            }
        }

        void host_Faulted(object sender, EventArgs e)
        {
            MyLog.writeError("WCF-Faulted");
        }

        void host_UnknownMessageReceived(object sender, UnknownMessageReceivedEventArgs e)
        {
            MyLog.writeError("WCF-UnknownMessageReceived");
        }

        void host_Opened(object sender, EventArgs e)
        {
            MyLog.writeInfo("WCF服务已启动,url:" + ServiceHost.BaseAddresses[0].AbsoluteUri.ToString());
        }

        void host_Closed(object sender, EventArgs e)
        {

            MyLog.writeInfo("WCF服务已停止");
        }

        public int? open()
        {
            try
            {
                if (ServiceHost.State == CommunicationState.Created)
                {
                    ServiceHost.Open();
                    return null;
                }
                return 1;
            }
            catch (System.Exception ex)
            {

                MyLog.writeError(ex.Message);

                throw new MyException(ex.Message);
            }
        }


        public int? close()
        {
            if (ServiceHost.State == CommunicationState.Opened)
            {
                ServiceHost.Close();
                return null;
            }
            else
            {
                return 1;
            }
        }
    }
}
